<?php

class Catalogue_Model_Attribute_Enum extends Catalogue_Model_Attribute {

    /**
     * Тип объекта
     * @var string
     */
    protected $_objectType = Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM;

	static function getInstance($identity) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM);
		return $storage->getOne($identity);
	}

	static function create($data) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM);
		return $storage->create($data);
	}

    static function getAll($where = null, $order = null, $limit = null, $offset = null) {
        if ($limit > 0 && $offset > 0) {
            $offset = ($limit * ($offset - 1));
        }
        $storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM);
        return $storage->getAll($where, $order, $limit, $offset);
    }
    
	static function getByCode($code) {
        $storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM);
        return $storage->getAll(array('code = ?' => $code));
    }

    function __construct($data, $options = null) {
        if ($data instanceof Catalogue_Model_Attribute) {
			$this->_htmlId = $data->getHtmlId();

			$params = $data->getParams();
            $data = $data->getData();

		} else {
			$params = null;
		}

        parent::__construct($data, $options);

		if (null !== $params) {
			$this->setParams($params);
		}
    }

	function save() {
		$storage = $this->_initStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM);
		return $storage->save($this);
	}

	function addItem($value, $desc = null, $popular = 0) {
        if (is_array($value)) {
            if (!isset($value['value'])) {
                return;
            }
            $fields = array_intersect_key($value, array('value' => null, 'description' => null, 'popular' => null));
        } else {
            $fields = array(
                'value' => (string)$value,
                'description' => (string)$desc,
                'popular' => (int)$popular
            );
        }

        $fields['attr_id'] = $this->getId();

		$item = Catalogue_Model_Attribute_Enum_Item::create($fields);
		return $item->save();
	}

	function changeItem($item, $newValue, $newDesc = null, $newPopular = 0) {
        if (!$item instanceof Catalogue_Model_Attribute_Enum_Item) {
            $item = Catalogue_Model_Attribute_Enum_Item::getInstance($item);
        }

        if (is_array($newValue)) {
            if (!isset($newValue['value'])) {
                return;
            }
            $data = array_intersect_key($newValue, array('value' => null, 'description' => null, 'popular' => null));
        } else {
            $data = array(
                'value' => (string)$newValue
            );

            if ($newDesc !== null) {
                $data['description'] = (string)$newDesc;
            }

            if ($newPopular > 0) {
                $data['popular'] = (int)$newPopular;
            }
        }

		$item->setData($data);
		return $item->save();
	}

	function removeItem($item) {
		if (!$item instanceof Catalogue_Model_Attribute_Enum_Item) {
            $item = Catalogue_Model_Attribute_Enum_Item::getInstance($item);
        }
        
		if ($item->getAttributeId() == $this->getId()) {
			return $item->delete();
		} else {
			throw new Workset_Model_Exception('Undefined attribute item in '.get_class($this));
		}
	}

	function removeItems() {
		$items = $this->getItems();
		foreach ($items as $item) {
			$item->delete();	
		}
	}

	function getItem($id) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM_ITEM);
		return $storage->getOne($id);
	}

	function getItems($filter = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {
		$storage = self::getDefaultStorage(Catalogue_Model_Object_Abstract::T_ATTRIBUTE_ENUM_ITEM);
		return $storage->getAll($this, $filter, $order, $limit, $offset, $offsetByPage);
	}

}

?>